﻿using PLC.Domain.Dto;
using PLC.Domain.Enum;
using PLC.Domain.Models;

namespace PLC.Domain.ScheduledTasks.Strategy.Trigger;

public class ValidateUpperEndPlateCodeStrategy:BaseTrigger
{
    public override EnumTrigger EnumTrigger { get; protected set; } = EnumTrigger.校验上端板条码;
    public override int TriggerEvent(PLCConfig plcConfig, DtoDataConfig trigger, PLCReadModel plcReadModel)
    {
        // 这边不存储数据
        foreach (var barcodeData in plcReadModel.BarcodeDatas)
        {
            barcodeData.SetValidResult(false);
        }
        // 如果触发的是等待   那么直接写入等待
        if (plcReadModel.TriggerValue == "1")
        {
            return (int)EnumWriteType.等待;
        }
        
        if (plcReadModel.BarcodeDatas.Count!=0 && plcReadModel.BarcodeDatas.All(p=>p.UpperEndPlateCode.Length>=(trigger.CodeLength??0)))
        {
            return (int)EnumWriteType.OK;
        }
        return (int)EnumWriteType.NG;
    }
}